﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model;
using l1cs.server.templates;
using System.Xml;
using System.Collections;
using l1cs.server.model.npc.action;

namespace l1cs.server
{
    public class GMCommandsConfig
    {
        private static Logger _log = Logger.getLogger( typeof( GMCommandsConfig ).FullName );

        private interface ConfigLoader
        {
            void load( XmlReader reader );
        }

        private abstract class ListLoaderAdapter : ConfigLoader
        {
            private String _listName;

            public ListLoaderAdapter( String listName )
            {
                _listName = listName;
            }

            public void load( XmlReader reader )
            {
                while ( reader.Read() )
                {
                    // 開始タグを発見した場合
                    if ( reader.NodeType == XmlNodeType.Element )
                    {
                        if ( string.Compare( reader.Name, _listName, true ) == 0 )
                        {
                            loadElement( reader );
                        }
                    }
                    else if ( reader.NodeType == XmlNodeType.EndElement )
                    {
                        break;
                    }
                }
            }

            public abstract void loadElement( XmlReader reader );
        }

        private class RoomLoader : ListLoaderAdapter
        {
            public RoomLoader() : base( "Room" ) { }

            override public void loadElement( XmlReader reader )
            {
                Hashtable attribute = L1NpcXmlParser.getAllAttribute( reader );
                String name = (string)attribute["Name"];
                int locX = Convert.ToInt32( (string)attribute["LocX"] );
                int locY = Convert.ToInt32( (string)attribute["LocY"] );
                int mapId = Convert.ToInt32( (string)attribute["MapId"] );
                ROOMS.Add( name.ToLower(), new L1Location( locX, locY, mapId ) );
            }
        }

        private class ItemSetLoader : ListLoaderAdapter
        {
            public ItemSetLoader() : base( "ItemSet" ) { }

            public L1ItemSetItem loadItem( XmlReader reader )
            {
                Hashtable attribute = L1NpcXmlParser.getAllAttribute( reader );
                int id = Convert.ToInt32( (string)attribute["Id"] );
                int amount = Convert.ToInt32( (string)attribute["Amount"] );
                int enchant = Convert.ToInt32( (string)attribute["Enchant"] );
                return new L1ItemSetItem( id, amount, enchant );
            }

            override public void loadElement( XmlReader reader )
            {
                Hashtable attribute = L1NpcXmlParser.getAllAttribute( reader );
                List<L1ItemSetItem> list = new List<L1ItemSetItem>();
                while ( reader.Read() )
                {
                    // 開始タグを発見した場合
                    if ( reader.NodeType == XmlNodeType.Element )
                    {
                        if ( string.Compare( reader.Name, "Item", true ) == 0 )
                        {
                            list.Add( loadItem( reader ) );
                        }
                    }
                    else if ( reader.NodeType == XmlNodeType.EndElement )
                    {
                        break;
                    }
                }
                String name = (string)attribute["Name"];
                ITEM_SETS.Add( name.ToLower(), list );
            }
        }

        //private static Dictionary <String, ConfigLoader> _loaders = new Dictionary<String, ConfigLoader>();
        //static {
        //    GMCommandsConfig instance = new GMCommandsConfig();
        //    _loaders.put("roomlist", instance.new RoomLoader());
        //    _loaders.put("itemsetlist", instance.new ItemSetLoader());
        //}

        public static Dictionary<String, L1Location> ROOMS = new Dictionary<String, L1Location>();
        public static Dictionary<String, List<L1ItemSetItem>> ITEM_SETS = new Dictionary<String, List<L1ItemSetItem>>();

        //private static Document loadXml(String file) {
        //    DocumentBuilder builder = DocumentBuilderFactory.newInstance()				.newDocumentBuilder();
        //    return builder.parse(file);
        //}

        public static void load()
        {
            // 「XmlReader」インスタンスの宣言(nullで初期化)
            XmlReader reader = null;
            // 特別なルールを設定する場合、「XmlReaderSettings」クラスを使用する。
            XmlReaderSettings settings = new XmlReaderSettings();
            // 検証は「整形式かどうか」の検査のみ
            settings.ConformanceLevel = ConformanceLevel.Document;

            // 「XmlReader」の作成
            reader = XmlReader.Create( "./data/xml/GmCommands/GMCommands.xml", settings );

            //NodeList nodes = doc.getDocumentElement().getChildNodes();
            //for (int i = 0; i < nodes.getLength(); i++) {
            //    ConfigLoader loader = _loaders.get(nodes.item(i).getNodeName().toLowerCase());
            //    if (loader != null) {
            //        loader.load((Element) nodes.item(i));
            //    }
            while ( reader.Read() )
            {
                // 開始タグを発見した場合
                if ( reader.NodeType == XmlNodeType.Element )
                {
                    if ( string.Compare( reader.Name, "roomlist", true ) == 0 )
                    {
                        ConfigLoader loader = new RoomLoader();
                        loader.load( reader );
                    }
                    if ( string.Compare( reader.Name, "itemsetlist", true ) == 0 )
                    {
                        ItemSetLoader loader = new ItemSetLoader();
                        loader.load( reader );
                    }
                }
            }
        }
    }
}


